package com.asj.house.controller;

import com.asj.house.client.HouseRoomClient;
import com.asj.house.domain.HouseRoom;
import com.asj.house.returns.RespCode;
import com.asj.house.returns.RespEntity;
import com.asj.house.returns.RespTable;
import com.asj.house.utils.OSSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * <p>
 * 房源管理 前端控制器
 * </p>
 *
 * @author asj
 * @since 2021-02-03
 */
@RestController
@RequestMapping("/house-room")
@Slf4j
@Api(value = "房源相关接口", tags = "房源相关接口")
public class HouseRoomController {

    @Resource
    private HouseRoomClient houseRoomClient;

    @Resource
    private OSSUtil ossUtil;

    @ApiOperation(value = "房源列表")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "page", value = "当前页数", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "limit", value = "每页数量", required = true)
    })
    public RespTable listHouseRoom(@RequestParam(value = "page", required = true) Integer page,
                                    @RequestParam(value = "limit", required = true) Integer limit) {
        RespTable respTable = houseRoomClient.listHouseRoom(page, limit);
        return respTable;
    }

    @GetMapping("/search")
    @ApiOperation(value = "搜索房源")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "page", value = "当前页数", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "limit", value = "每页数量", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "name", value = "小区名称", required = true)
    })
    public RespTable searchHouseRoom(@RequestParam(value = "page", required = true) Integer page,
                                     @RequestParam(value = "limit", required = true) Integer limit,
                                     @RequestParam(value = "name", required = true) String name) {
        RespTable respTable = houseRoomClient.searchHouseRoom(page, limit, name);
        return respTable;
    }

    /**
     * OSS文件上传
     * */
    @RequestMapping("/upload")
    @ApiOperation(value = "上传房源照片")
    public RespEntity upload(MultipartFile file){
        String fileName = ossUtil.getFileName("house/room/", file.getOriginalFilename());
        String url = ossUtil.getUrl(fileName);
        try {
            byte[] bytes = file.getBytes();
            ossUtil.upload(fileName, bytes);
            return new RespEntity(RespCode.SUCCESS, url);
        } catch (Exception e) {
            log.error("文件上传失败", e.getMessage());
            return new RespEntity(RespCode.UPLOAD_ERROR);
        }
    }

    @PostMapping("/insert")
    @ApiOperation(value = "新增房源")
    public RespEntity insertRoom(HouseRoom houseRoom) {
        houseRoomClient.insertRoom(houseRoom);
        return new RespEntity(RespCode.SUCCESS);
    }

    @PutMapping("/update")
    @ApiOperation("编辑房源")
    public RespEntity updateRoom(HouseRoom houseRoom) {
        houseRoomClient.updateRoom(houseRoom);
        return new RespEntity(RespCode.SUCCESS);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除房源")
    public RespEntity deleteRoom(@RequestParam("roomId") Integer roomId) {
        houseRoomClient.deleteRoom(roomId);
        return new RespEntity(RespCode.SUCCESS);
    }

    @DeleteMapping("/deleteBatch")
    @ApiOperation(value = "批量删除房源")
    public RespEntity deleteBatchRoom(@RequestParam("roomIds") Integer[] roomIds) {
        houseRoomClient.deleteBatchRoom(roomIds);
        return new RespEntity(RespCode.SUCCESS);
    }

}
